//
//  UIShowBtnTextFieldViewController.swift
//  SwiftRx研究
//
//  Created by 世霖 on 2020/7/16.
//  Copyright © 2020 世霖. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class UIShowBtnTextFieldViewController: UIViewController {

    @IBOutlet weak var showSwitch: UISwitch!
    let disposeBag = DisposeBag()
    @IBOutlet weak var showBtn: UIButton!
    @IBOutlet weak var showTextField: UITextField!
    
    
    @IBOutlet weak var showTextLabel: UILabel!
    
    var userModel = UserViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
//        showBtn.rx.tap
//        .subscribe(onNext: {[weak self ] in
//
//            self?.showMessage("点击")
//        })
//        .disposed(by: disposeBag )
//
        
        showBtn.rx.tap.bind { [weak self] in

            self?.showMessage("amnni")
        }.disposed(by: disposeBag)

//
//        //创建一个计时器（每1秒发送一个索引数）
//        let timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
//
//        timer.map {_ in
//            "==="
//        }
//        .bind(to: showBtn.rx.title(for: .normal))
//        .disposed(by: disposeBag)
//
//        按钮的富文本标题
        
        //创建一个计时器（每1秒发送一个索引数）
//              let timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
//
//              //将已过去的时间格式化成想要的字符串，并绑定到button上
//              timer.map(formatTimeInterval)
//                  .bind(to: showBtn.rx.attributedTitle())
//                  .disposed(by: disposeBag)
//
        
        showSwitch.rx.isOn.asObservable()
            .subscribe(onNext: { (event) in
                print(event)
            })
        .disposed(by: disposeBag)
        
        showSwitch.rx.isOn
            .bind(to: showBtn.rx.isEnabled)
        .disposed(by: disposeBag)
        
        
        
        userModel.userName.asObservable().bind(to: showTextField.rx.text)
        .disposed(by: disposeBag)
        
        showTextField.rx.text.orEmpty.bind(to: userModel.userName).disposed(by: disposeBag)
        
        //label绑定
        userModel.userInfo.bind(to: showTextLabel.rx.text).disposed(by: disposeBag)
        
//        自定义双向绑定操作符（operator）
        
        
        
        
        
        
        
        
    }
     //显示消息提示框
       func showMessage(_ text: String) {
           let alertController = UIAlertController(title: text, message: nil, preferredStyle: .alert)
           let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
           alertController.addAction(cancelAction)
           self.present(alertController, animated: true, completion: nil)
       }
    //将数字转成对应的富文本
      func formatTimeInterval(ms: NSInteger) -> NSMutableAttributedString {
          let string = String(format: "%0.2d:%0.2d.%0.1d",
                              arguments: [(ms / 600) % 600, (ms % 600 ) / 10, ms % 10])
          //富文本设置
          let attributeString = NSMutableAttributedString(string: string)
          //从文本0开始6个字符字体HelveticaNeue-Bold,16号
        attributeString.addAttribute(NSAttributedString.Key.font,
                                       value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,
                                       range: NSMakeRange(0, 5))
          //设置字体颜色
        attributeString.addAttribute(NSAttributedString.Key.foregroundColor,
                                       value: UIColor.white, range: NSMakeRange(0, 5))
          //设置文字背景颜色
        attributeString.addAttribute(NSAttributedString.Key.backgroundColor,
                                       value: UIColor.orange, range: NSMakeRange(0, 5))
          return attributeString
      }
    
    
    
    
    
    
    
    
}
